package sdp.group14.management.tenant;

import java.text.SimpleDateFormat;
import java.util.Date;

import sdp.group14.R;
import sdp.group14.appdata.AppEnumData;
import sdp.group14.appdata.Event;
import sdp.group14.appdata.Request;
import sdp.group14.management.RequestManagerActivity;
import sdp.group14.management.HousingManagerApplication;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class TenantAddRequestActivity extends RequestManagerActivity {
	
	private EditText commentEditText, descripEditText, causeEditText;
	private CheckBox allowEnter;
	private boolean changesPending;
	private AlertDialog unsavedChangesDialog;
	public static Spinner spinner;
	private ViewFlipper flipper;
	private TextView dateTextView;
	SimpleDateFormat currentTime;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tenant_add_request);
       
        flipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);
        spinner = (Spinner) findViewById(R.id.spinner);
		ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.item, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
        
        setUpViews();
    }

	protected void addEvent() {	
		
		int entryID = 1;				
		
		String status = "NEW";
		String category = "" + spinner.getSelectedItem();		
		int allowStaffEnter = (allowEnter.isChecked()) ? 1 : 2; 
		String description = descripEditText.getText().toString();
		String cause = causeEditText.getText().toString();
		String comments = commentEditText.getText().toString();
		String eventDate = "";
		String dayModified = "";
		String dayCreated = "";

		Event event = new Event(entryID, allowStaffEnter, status,
				category, description, cause, comments,
				eventDate, dayModified, dayCreated);
		
		((HousingManagerApplication)getApplication()).addEvent(event);
		finish();
	}

	protected void cancel() {
		if (changesPending) {
			unsavedChangesDialog = new AlertDialog.Builder(this)
				.setTitle(R.string.unsaved_changes_title)
				.setMessage(R.string.unsaved_changes_message)
				.setPositiveButton(R.string.add_request, new AlertDialog.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						addEvent();
					}
				})
				.setNeutralButton(R.string.discard, new AlertDialog.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				})
				.setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						unsavedChangesDialog.cancel();
					}
				})
				.create();
			unsavedChangesDialog.show();
		} else {
			finish();
		}
	}

	private void setUpViews() {
		currentTime = new SimpleDateFormat("dd/MM/yyyy/ HH:mm:ss");
		commentEditText = (EditText)findViewById(R.id.edit_comment);
		spinner = (Spinner) findViewById(R.id.spinner);
		descripEditText = (EditText) findViewById(R.id.edit_description);
		causeEditText = (EditText)findViewById(R.id.edit_cause);
		allowEnter = (CheckBox) findViewById(R.id.checkbx_allow_enter);
		dateTextView = (TextView) findViewById(R.id.text_date);
		
		dateTextView.setText(dateTextView.getText() + currentTime.format(new Date()));
		commentEditText.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				changesPending = true;
			}
			public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
			public void afterTextChanged(Editable s) { }
		});
		descripEditText.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				changesPending = true;
			}
			public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
			public void afterTextChanged(Editable s) { }
		});
		causeEditText.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				changesPending = true;
			}
			public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
			public void afterTextChanged(Editable s) { }
		});
	}	
    
	/*
	 * Next button is clicked
	 */
    public void onClickNext(View view) {
    	flipper.showNext();
    }
    
    /*
	 * Previous button is clicked
	 */
    public void onClickPrevious(View view) {
    	flipper.showPrevious();
    }
    
    /*
	 * Cancel button is clicked
	 */
    public void onClickCancel(View view) {
    	cancel();
    }
    
    /*
	 * Add task button is clicked
	 */
    public void onClickAdd(View view) {
    	addEvent();
    }
    	
    
}
